package com.abe.bathsystem.android.service;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import com.abe.bathsystem.android.v3.MainBathAbsActivity;
import com.abe.bathsystem.base.ManagerAPP;
import com.abe.bathsystem.entity.http.HttpGPushEntity;
import com.abe.bathsystem.utils.ConstantBath;
import com.abe.libcore.utils.normal.CommonUtils;
import com.google.gson.Gson;
import com.tencent.android.tpush.XGPushBaseReceiver;
import com.tencent.android.tpush.XGPushClickedResult;
import com.tencent.android.tpush.XGPushRegisterResult;
import com.tencent.android.tpush.XGPushShowedResult;
import com.tencent.android.tpush.XGPushTextMessage;

public class XGPushBathReceiver extends XGPushBaseReceiver {
    public static final String MESSAGE_RECEIVED_ACTION = "com.abe.bath.push";
    public static final String MESSAGE_KEY = "push";

    // 通知展示
    @Override
    public void onNotifactionShowedResult(Context context, XGPushShowedResult notifiShowedRlt) {
    }

    //反注册的回调
    @Override
    public void onUnregisterResult(Context context, int errorCode) {

    }

    //设置tag的回调
    @Override
    public void onSetTagResult(Context context, int errorCode, String tagName) {

    }

    //删除tag的回调
    @Override
    public void onDeleteTagResult(Context context, int errorCode, String tagName) {

    }

    @Override
    public void onNotifactionClickedResult(Context context, XGPushClickedResult message) {
    }

    //注册的回调
    @Override
    public void onRegisterResult(Context context, int errorCode, XGPushRegisterResult message) {
    }

    @Override
    public void onTextMessage(Context context, XGPushTextMessage xgPushTextMessage) {
        CommonUtils.w("XGPushTextMessage********************" + xgPushTextMessage.toString());
        StringBuilder builder = new StringBuilder();
        String newLine = "\r\n";
        builder.append("================================================================================================").append(newLine);
        builder.append("push:all**********").append(xgPushTextMessage.toString()).append(newLine);
        String data = xgPushTextMessage.getContent();
        if (TextUtils.isEmpty(data)) {
            CommonUtils.saveLocalDailyLog(MainBathAbsActivity.getRealTime(), builder.toString(), ConstantBath.LOG_PUSH_XG_PATH);
            return;
        }
        builder.append("push:content******").append(data).append(newLine);
        CommonUtils.saveLocalDailyLog(MainBathAbsActivity.getRealTime(), builder.toString(), ConstantBath.LOG_PUSH_XG_PATH);
        HttpGPushEntity entity = new Gson().fromJson(data, HttpGPushEntity.class);
        if (entity == null) return;
        Bundle bundle = new Bundle();
        bundle.putSerializable(MESSAGE_KEY, entity);
        CommonUtils.sendLocalBroadcast(MESSAGE_RECEIVED_ACTION, bundle);
    }
}

